import requests
import os
from urllib.parse import urlencode
from pyquery import PyQuery as pq


base_url = 'https://m.weibo.cn/api/container/getIndex?'
headers = {
	'Host':'m.weibo.cn',
    'Referer': 'https://m.weibo.cn/u/1402400261',
    'User-Agent':'Mozilla/s.o (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Sa ari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
}

def page_json(page):
	params = {
    'type':'uid',
    'value':'1402400261',
    'containerid':'1076031402400261',
    'page':page
	}
	url = base_url + urlencode(params)
	print(url)
	try:
		res = requests.get(url, headers=headers)
		if res.status_code == 200:
			# print(res.json())
			return res.json()
	except requests.ConnectionError as e:
		print('Error', e.args)

def parse_page(json):
	if json:
		items = json.get('data').get('cards')
		for item in items:
			item = item.get('mblog')
			weibo={}
			weibo['id'] = item.get('id')
			weibo['内容'] = pq(item.get('text')).text()
			yield weibo

file_path = r'./wb_spider1_text2.txt'
file = open(file_path, 'w', encoding='utf-8')


if __name__ == '__main__':
	for page in range(1,6):
		json = page_json(page)
		results = parse_page(json)
		for result in results:
			file.write(str(result) + "\n")
